package com.jidong.test_springboot.web;

import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jidong.test_springboot.dao.UserDao;
import com.jidong.test_springboot.entity.Router;
import com.jidong.test_springboot.entity.User;
import com.jidong.test_springboot.service.RouterService;
import com.jidong.test_springboot.util.JWTUtil;
import com.jidong.test_springboot.vo.ResultData;

@RestController
public class PermissionServlet {
	@Autowired
	private RouterService routerService;
	@Autowired
	private UserDao userDao;

	/**
	 * 这里给前端的是用户的用户信息以及用户的所在角色所有的按钮权限
	 * */
	@RequestMapping("/user/info")
	public ResultData getUserInfo(HttpServletRequest request) {
		
		//读出请求头中的token
		String token=request.getHeader("Access-Token");
		//解析token得到用户名
		String username=(String)JWTUtil.getClaim(token, "username");
		
		User u=userDao.selectOne(new QueryWrapper<User>().eq("uname", username));
		
		JSONObject userInfo=new JSONObject();
		userInfo.put("username", u.getName());
		userInfo.put("avatar", u.getAvatar());
		//获取用户名的所有按钮权限
		Set<String> btnPermissions= userDao.selectPermissionByUname(username);
		Set<String> allBtnPermissions=userDao.selectAllPermissions();
		JSONObject result=new JSONObject();
		result.put("userInfo", userInfo);
		result.put("btnPermissions", btnPermissions);
		result.put("allBtnPermissions", allBtnPermissions);
		
		ResultData data=new ResultData();
		data.setCode(200);
		data.setMsg("请求成功");
		data.setResult(result);//把用户信息以及按钮权限交给data
		System.out.println(data);
		return data;
	}
	
	
	
	
	/**获取路由的请求**/
	@RequestMapping("/user/dynamic-menu")
	public ResultData dynamicMenu(HttpServletRequest request) {
		//读出请求头中的token
		String token=request.getHeader("Access-Token");
		//解析token得到用户名
		String username=(String)JWTUtil.getClaim(token, "username");
		//根据用户名得到路由
		Set<Router> routes=routerService.selectRouterByUname(username);
		//根据路由数据集合拼成以下格式的json
		
		System.out.println(routes);
		
		JSONArray menu=new JSONArray();
		
		generateMenu(menu,routes,null);//调用这个方法，把routes转为json数组放进menu
		
		
		
		/*JSONObject c1=new JSONObject();
		c1.put("title", "分析页");
		c1.put("key", "analysis");
		JSONObject c2=new JSONObject();
		c2.put("title", "监控页");
		c2.put("key", "monitor");
		JSONObject c3=new JSONObject();
		c3.put("title", "工作台");
		c3.put("key", "workplace");
		JSONArray menu1_Child=new JSONArray();
		menu1_Child.add(c1);
		menu1_Child.add(c2);
		menu1_Child.add(c3);
		JSONObject menu1=new JSONObject();
		menu1.put("title", "仪表盘");
		menu1.put("key", "dashboard");
		menu1.put("component", "RouteView");
		menu1.put("icon", "dashboard");
		menu1.put("children", menu1_Child);
		JSONObject menu2=new JSONObject();
		menu2.put("title", "系统管理");
		menu2.put("key", "system");
		menu2.put("component", "PageView");
		menu2.put("icon", "setting");
		menu2.put("children", menu1_Child);
		
		
		JSONArray children=new JSONArray();
		children.add(menu1);
		children.add(menu2);
		
		JSONObject index=new JSONObject();
		index.put("title", "首页");
		index.put("key", "");
		index.put("name", "index");
		index.put("component", "BasicLayout");
		index.put("redirect", "/dashboard/workplace");
		index.put("children", children);
		
		
		JSONArray menu=new JSONArray();
		menu.add(index);*/
		
		ResultData data=new ResultData();
		data.setCode(200);
		data.setMsg("请求成功");
		data.setResult(menu);//把用户信息交给data
		return data;
	}


	/*
	 * Router(id=2, type=0, title=仪表盘, key=dashboard, component=RouteView, name=null, redirect=null, icon=dashboard, parentId=1, isParent=1), 
	 * Router(id=1, type=0, title=首页, key=null, component=BasicLayout, name=index, redirect=/dashboard/workplace, icon=null, parentId=0, isParent=1), 
	 * Router(id=6, type=0, title=工作台, key=workplace, component=null, name=null, redirect=null, icon=null, parentId=2, isParent=0), 
	 * Router(id=5, type=0, title=监控, key=monitor, component=null, name=null, redirect=null, icon=null, parentId=2, isParent=0), 
	 * Router(id=4, type=0, title=分析, key=analysis, component=null, name=null, redirect=null, icon=null, parentId=2, isParent=0)]
	 * 
	 * */
	/**生成一颗menu菜单树，最终结果 menu就是那棵树，routes是路由表**/
	private void generateMenu(JSONArray menu, Set<Router> routes,Router parent) {
		for(Router route:routes) {
			JSONObject j_route=changeRoute2Json(route);//把一个路由转为json
			if(parent==null) {
				if(route.getIsParent().equals("1")) {
					JSONArray children=new JSONArray();
					j_route.put("children", children);
					generateMenu(children, routes, route);
				}
				if(route.getParentId()==0) {
					menu.add(j_route);
				}
			}else {
				if(route.getParentId()==parent.getId()) {
					menu.add(j_route);
					if(route.getIsParent().equals("1")) {
						JSONArray children=new JSONArray();
						j_route.put("children", children);
						generateMenu(children, routes, route);
					}
				}
			}
		}
		
		
	}



	/**把单个路由转为一个json*/
	private JSONObject changeRoute2Json(Router route) {
		JSONObject obj=new JSONObject();
		obj.put("title", route.getTitle());
		obj.put("key", route.getKey());
		obj.put("name", route.getName());
		obj.put("component", route.getComponent());
		obj.put("redirect", route.getRedirect());
		return obj;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
